﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace wfGeneradorWeb
{
    public partial class Form1 : Form
    {
        public static string nombreImg2 = "";
        public static string rutaImg2 = "";
        public static string rutaImg1 = "";
        public static string nombreImg1 = "";
        public static string nombreImg3 = "";
        public static string rutaImg3 = "";
        public static string nombreImg4 = "";
        public static string rutaImg4 = "";
        public static string titulo = "";
        public static string descripcion = "";
        public static bool img1, img2, img3, img4;
        public static List<String[]> ftps = new List<string[]>();
        servicio.authServiceSoapClient serv = new servicio.authServiceSoapClient();

        public Form1()
        {
            InitializeComponent();
            img1 = false;
            img2 = false;
            img3 = false;
            img4 = false;
            string[][] result = serv.listaFtp("","","");
            ftps = result.ToList();
            /*
            String[] miftp=new String[4];
            miftp[0] = "serv";
            miftp[1] = "usuario";
            miftp[2] = "pass";
            miftp[3] = "www/pubexterna";
            ftps.Add(miftp);
            */


        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                nombreImg1 = System.IO.Path.GetFileName(ofd.FileName);
                rutaImg1 = System.IO.Path.GetDirectoryName(ofd.FileName);
                txtRutaImg1.Text = rutaImg1 + "\\" + nombreImg1;
                txtNombreImg1.Text = nombreImg1;
                button3.Enabled = true;
                txtNombreImg2.Enabled = true;
            }            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog ofd = new FolderBrowserDialog();
            string rutaDestino = "";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                rutaDestino = ofd.SelectedPath;
                txtDestino.Text = rutaDestino;
                button1.Enabled = true;
            }            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            StreamReader objReader = new StreamReader("modelo.html");
            StringBuilder sb = new StringBuilder();
            
            string sLine = "";
            List<String> arrText = new List<string>();
            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                sb.Append(sLine);
            }
            sb.Replace("[titulo]", txtTitulo.Text);
            sb.Replace("[contenido]", txtTexto.Text);
            sb.Replace("[img1]", txtNombreImg1.Text);
            sb.Replace("[img2]", txtNombreImg2.Text);
            sb.Replace("[img3]", txtNombreImg3.Text);
            sb.Replace("[img4]", txtNombreImg4.Text);
            objReader.Close();

            if (txtRutaImg1.Text != "" && txtNombreImg1.Text != "")
            {
                System.IO.File.Copy(txtRutaImg1.Text, Path.Combine(txtDestino.Text, txtNombreImg1.Text), true);
                img1 = true;
            }
            if (txtRutaImg2.Text != "" && txtNombreImg2.Text != "")
            {
                img2 = true;
                System.IO.File.Copy(txtRutaImg2.Text, Path.Combine(txtDestino.Text, txtNombreImg2.Text), true);
            }
            if (txtRutaImg3.Text != "" && txtNombreImg3.Text != "")
            {
                img3 = true;
                System.IO.File.Copy(txtRutaImg3.Text, Path.Combine(txtDestino.Text, txtNombreImg3.Text), true);
            }
            if (txtRutaImg4.Text != "" && txtNombreImg4.Text != "")
            {
                img4 = true;
                System.IO.File.Copy(txtRutaImg4.Text, Path.Combine(txtDestino.Text, txtNombreImg4.Text), true);
            }
            System.IO.File.Copy(Path.GetFullPath("bootstrap.min.css"), Path.Combine(txtDestino.Text, "bootstrap.min.css"), true);
            System.IO.File.WriteAllText(Path.Combine(txtDestino.Text, txtNombreHtml.Text + ".html"), sb.ToString());

            MessageBox.Show("Archivos generados correctamente","Aviso");
            groupBox3.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                nombreImg2 = System.IO.Path.GetFileName(ofd.FileName);
                rutaImg2 = System.IO.Path.GetDirectoryName(ofd.FileName);
                txtRutaImg2.Text = rutaImg2 + "\\" + nombreImg2;
                txtNombreImg2.Text = nombreImg2;
                button4.Enabled = true;
                txtNombreImg3.Enabled = true;
            }            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                nombreImg3 = System.IO.Path.GetFileName(ofd.FileName);
                rutaImg3 = System.IO.Path.GetDirectoryName(ofd.FileName);
                txtRutaImg3.Text = rutaImg3 + "\\" + nombreImg3;
                txtNombreImg3.Text = nombreImg3;
                button5.Enabled = true;
                txtNombreImg4.Enabled = true;
            }
           
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                nombreImg4 = System.IO.Path.GetFileName(ofd.FileName);
                rutaImg4 = System.IO.Path.GetDirectoryName(ofd.FileName);
                txtRutaImg4.Text = rutaImg4 + "\\" + nombreImg4;
                txtNombreImg4.Text = nombreImg4;
            }            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                StreamReader objReader = new StreamReader("modelo.html");
                StringBuilder sb = new StringBuilder();
                string archivo = Path.Combine(Path.GetTempPath(), "articulo.html");
                string sLine = "";
                List<String> arrText = new List<string>();
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    sb.Append(sLine);
                }
                sb.Replace("[titulo]", txtTitulo.Text);
                sb.Replace("[contenido]", txtTexto.Text);
                sb.Replace("[img1]", txtNombreImg1.Text);
                sb.Replace("[img2]", txtNombreImg2.Text);
                sb.Replace("[img3]", txtNombreImg3.Text);
                sb.Replace("[img4]", txtNombreImg4.Text);
                objReader.Close();

                if (txtRutaImg1.Text != "" && txtNombreImg1.Text != "")
                    System.IO.File.Copy(txtRutaImg1.Text, Path.Combine(Path.GetTempPath(), txtNombreImg1.Text), true);
                if (txtRutaImg2.Text != "" && txtNombreImg2.Text != "")
                    System.IO.File.Copy(txtRutaImg2.Text, Path.Combine(Path.GetTempPath(), txtNombreImg2.Text), true);
                if (txtRutaImg3.Text != "" && txtNombreImg3.Text != "")
                    System.IO.File.Copy(txtRutaImg3.Text, Path.Combine(Path.GetTempPath(), txtNombreImg3.Text), true);
                if (txtRutaImg4.Text != "" && txtNombreImg4.Text != "")
                    System.IO.File.Copy(txtRutaImg4.Text, Path.Combine(Path.GetTempPath(), txtNombreImg4.Text), true);

                System.IO.File.Copy(Path.GetFullPath("bootstrap.min.css"), Path.Combine(Path.GetTempPath(), "bootstrap.min.css"), true);
                System.IO.File.WriteAllText(Path.Combine(Path.GetTempPath(), "articulo.html"), sb.ToString());
            }
            catch (Exception ex) { 
            
            }
            frmNavegador ventana = new frmNavegador();
            ventana.Show();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ftps.Count; i++)
            {
                string dest = ftps[i][3];
                ftp objFtp = new ftp(ftps[i][0], ftps[i][1], ftps[i][2]);
                if (!objFtp.verificaExitencia(dest, txtNuevaCarpeta.Text))
                    objFtp.CrearDirectorio(dest, txtNuevaCarpeta.Text);
                dest += "/" + txtNuevaCarpeta.Text;
                objFtp.UploadFTP(Path.Combine(txtDestino.Text, txtNombreHtml.Text + ".html"), dest);

                objFtp.UploadFTP(Path.Combine(txtDestino.Text, "bootstrap.min.css"), dest);

                if (img1)
                    objFtp.UploadFTP(Path.Combine(txtDestino.Text, txtNombreImg1.Text), dest);
                if (img2)
                    objFtp.UploadFTP(Path.Combine(txtDestino.Text, txtNombreImg2.Text), dest);
                if (img3)
                    objFtp.UploadFTP(Path.Combine(txtDestino.Text, txtNombreImg3.Text), dest);
                if (img4)
                    objFtp.UploadFTP(Path.Combine(txtDestino.Text, txtNombreImg4.Text), dest);
            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
